{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ecedf247",
   "metadata": {},
   "source": [
    "--------\n",
    "* 记录人：纪冰儿\n",
    "* 记录时间：2021-12-1\n",
    "* week13\n",
    "* 主要参考文档：[python官方文档](http://python.org/doc)\n",
    "\n",
    "\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51c9dba5",
   "metadata": {},
   "source": [
    "| set集合的方法      | Description | 具体使用     | 注意事项   | 使用结果   |\n",
    "| :---        |    :----  |       :--- |       :--- |       :--- |\n",
    "|set()       | 创建集合   |A=set()>A |集合具有唯一性；集合是无序的，集合用大括号包围|A=set(abcd)>A(a,b,c,d)|\n",
    "|add()       |将元素添加到集合中。|B = A.add() |当元素已在集合内时添加无效|B = A.add(q)>{q+原有的元素}|\n",
    "|update()    |添加多个元素到集合中   |set.update([a,b])|参数可以是列表，元组，字典等,元素可以有多个，用逗号隔开|B = A.set.update([a,b])|\n",
    "|clear()      |将集合内所有的元素删除|B = A.clear() |一般不常用 |B = A.clear()>set()|\n",
    "|copy()      |复制一个集合副本| B = A.copy() |—— |B = A.copy()> B{...}|\n",
    "|union()    |并集，将两个集合合并|C = A.union(set(B))|——|C{‘...’}|\n",
    "|difference   |差集，返回多个集合中的不同元素至一个新集合中|D = A.difference(set(B))或set_1 - ser_2|---|D = A.difference(set(B))> D{...}|\n",
    "|discard    |删除集合中指定的元素|B = A.discard('...')|如果元素不存在，不会发生错误。|A{\"...\"}|\n",
    "|intersection   |交集，返回多个集合中的相同元素至一个新集合中|I = A.intersection(set(B))或set_1 & set_2|——|I{‘...’}|\n",
    "|isdisjoint()   |判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False|C = B.isdisjoint(A)|——|True/Flase|\n",
    "|issubset()   |判断指定集合是否为该方法参数集合的子集|C = B.issubset(A)|——|True/Flase||\n",
    "|issuperset() |判断该方法的参数集合是否为指定集合的子集|C = B.issuperset(A)|——|True/Flase|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "331a8cb2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class set in module builtins:\n",
      "\n",
      "class set(object)\n",
      " |  set() -> new empty set object\n",
      " |  set(iterable) -> new set object\n",
      " |  \n",
      " |  Build an unordered collection of unique elements.\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __and__(self, value, /)\n",
      " |      Return self&value.\n",
      " |  \n",
      " |  __contains__(...)\n",
      " |      x.__contains__(y) <==> y in x.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __iand__(self, value, /)\n",
      " |      Return self&=value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __ior__(self, value, /)\n",
      " |      Return self|=value.\n",
      " |  \n",
      " |  __isub__(self, value, /)\n",
      " |      Return self-=value.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __ixor__(self, value, /)\n",
      " |      Return self^=value.\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __or__(self, value, /)\n",
      " |      Return self|value.\n",
      " |  \n",
      " |  __rand__(self, value, /)\n",
      " |      Return value&self.\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Return state information for pickling.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __ror__(self, value, /)\n",
      " |      Return value|self.\n",
      " |  \n",
      " |  __rsub__(self, value, /)\n",
      " |      Return value-self.\n",
      " |  \n",
      " |  __rxor__(self, value, /)\n",
      " |      Return value^self.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      S.__sizeof__() -> size of S in memory, in bytes\n",
      " |  \n",
      " |  __sub__(self, value, /)\n",
      " |      Return self-value.\n",
      " |  \n",
      " |  __xor__(self, value, /)\n",
      " |      Return self^value.\n",
      " |  \n",
      " |  add(...)\n",
      " |      Add an element to a set.\n",
      " |      \n",
      " |      This has no effect if the element is already present.\n",
      " |  \n",
      " |  clear(...)\n",
      " |      Remove all elements from this set.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      Return a shallow copy of a set.\n",
      " |  \n",
      " |  difference(...)\n",
      " |      Return the difference of two or more sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in this set but not the others.)\n",
      " |  \n",
      " |  difference_update(...)\n",
      " |      Remove all elements of another set from this set.\n",
      " |  \n",
      " |  discard(...)\n",
      " |      Remove an element from a set if it is a member.\n",
      " |      \n",
      " |      If the element is not a member, do nothing.\n",
      " |  \n",
      " |  intersection(...)\n",
      " |      Return the intersection of two sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in both sets.)\n",
      " |  \n",
      " |  intersection_update(...)\n",
      " |      Update a set with the intersection of itself and another.\n",
      " |  \n",
      " |  isdisjoint(...)\n",
      " |      Return True if two sets have a null intersection.\n",
      " |  \n",
      " |  issubset(...)\n",
      " |      Report whether another set contains this set.\n",
      " |  \n",
      " |  issuperset(...)\n",
      " |      Report whether this set contains another set.\n",
      " |  \n",
      " |  pop(...)\n",
      " |      Remove and return an arbitrary set element.\n",
      " |      Raises KeyError if the set is empty.\n",
      " |  \n",
      " |  remove(...)\n",
      " |      Remove an element from a set; it must be a member.\n",
      " |      \n",
      " |      If the element is not a member, raise a KeyError.\n",
      " |  \n",
      " |  symmetric_difference(...)\n",
      " |      Return the symmetric difference of two sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in exactly one of the sets.)\n",
      " |  \n",
      " |  symmetric_difference_update(...)\n",
      " |      Update a set with the symmetric difference of itself and another.\n",
      " |  \n",
      " |  union(...)\n",
      " |      Return the union of sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in either set.)\n",
      " |  \n",
      " |  update(...)\n",
      " |      Update a set with the union of itself and others.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cca46b6a",
   "metadata": {},
   "source": [
    "# 集合\n",
    "* 集合的特点：\n",
    "> 1.集合不允许重复；2.集合用大括号包围；3.集合是无序的\n",
    "* 集合的方法\n",
    "> 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "097ac5d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:kitty\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('provide a word to search for vowels:')\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac04185f",
   "metadata": {},
   "source": [
    "* 从序列创建代码\n",
    "* set 函数方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5244a2bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = {'a','e','i','o','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "229ba7b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set 函数方法用于创建集合\n",
    "vowels2 = set('aaeiou')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9c1d1e49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2b3a0a6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集union\n",
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8ca12fe3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'e', 'f', 'g', 'p', 'u', 'x'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ；练习：\n",
    "A = set('edgup')\n",
    "B = 'fpxup'\n",
    "C = A.union(set(B))\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "88fff14d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['d', 'e', 'f', 'g', 'p', 'u', 'x']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用sorted 函数将集合转换为一个有序列表\n",
    "C_list = sorted(list(C))\n",
    "C_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f91a8274",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'e', 'g'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集difference\n",
    "D = A.difference(set(B))\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2b952b9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'p', 'u'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集intersection \n",
    "I = A.intersection(set(B))\n",
    "I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "00c0652d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:kitty\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'intersection'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-7f6939952242>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mvowels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'aeiou'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mword\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'provide a word to search for vowels:'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mfound\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvowels\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mintersection\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mvowel\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfound\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvowel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'str' object has no attribute 'intersection'"
     ]
    }
   ],
   "source": [
    "vowels = ('aeiou')\n",
    "word = input('provide a word to search for vowels:')\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91fd3009",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66589955",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "398e3307",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f999e410",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
